package com.sk.sk_frame_cloud.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sk.sk_frame_cloud.config.DynamicDataSourceHolder;
import com.sk.sk_frame_cloud.constants.CommonConstant;
import com.sk.sk_frame_cloud.entity.Functions;
import com.sk.sk_frame_cloud.entity.SysProFunInfo;
import com.sk.sk_frame_cloud.mapper.SysProFunInfoMapper;
import com.sk.sk_frame_cloud.param.SysFunctionParam;
import com.sk.sk_frame_cloud.res.CommonResponse;
import com.sk.sk_frame_cloud.service.IFunctionsService;
import com.sk.sk_frame_cloud.service.ISysProFunInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.sk_frame_cloud.service.ISysProFunRelationService;
import com.sk.sk_frame_cloud.service.ISysProManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系统功能信息列表 服务实现类
 * </p>
 *
 * @author jiaok
 * @since 2023-12-20
 */
@Service
public class SysProFunInfoServiceImpl extends ServiceImpl<SysProFunInfoMapper, SysProFunInfo> implements ISysProFunInfoService {
    @Resource
    private ISysProManageService iSysProManageService;
    @Resource
    private ISysProFunRelationService iSysProFunRelationService;

    @Resource
    private IFunctionsService iFunctionsService;

    @Override
    public CommonResponse checkInfoIsExist(SysFunctionParam functionParam) {

        HashMap<String, String> res = new HashMap<>();

        SysProFunInfo nick_name = this.getOne(new QueryWrapper<SysProFunInfo>().eq("nick_name", functionParam.getNickname()));


        SysProFunInfo fun_code = this.getOne(new QueryWrapper<SysProFunInfo>().eq("fun_code", functionParam.getCode()));

        if (Objects.nonNull(nick_name)){
            res.put("nickname","当前功能名称已存在");
        } else if (Objects.nonNull(fun_code)){
            res.put("code","当前功能代码已存在");
        }

        if (CollectionUtils.isEmpty(res)){
            return CommonResponse.ok();
        }else {
            return CommonResponse.error(res);
        }
    }

    @Override
    public CommonResponse addFunction(SysFunctionParam functionParam) {
        String dbCode = iSysProManageService.getProCode(functionParam.getProSid());
        if (StringUtils.isBlank(dbCode)){
            return CommonResponse.error("proSid 不能为空");
        }
        //首先检测当前所有项目中是否已经存在当前功能，对应的code值
        CommonResponse commonResponse = checkInfoIsExist(functionParam);
        if (commonResponse.getCode().equals(CommonConstant.SC_INTERNAL_SERVER_ERROR_500)){
            return commonResponse;
        }
        functionParam.setFunType(getNewFunType());
        // TODO: 2023-12-20  获取当前操作的数据库，想办法优化一下
        //操作父项目，
        operaManageFunction(functionParam);
        //操作子项目---切换数据库
        iFunctionsService.operaFollowFunction(dbCode,functionParam);

        return CommonResponse.ok("功能新增成功");
    }

    @Override
    public CommonResponse getProManageFunList() {

        List<SysProFunInfo> proFunInfoList = this.list();
        return CommonResponse.ok(proFunInfoList);
    }

    @Override
    public CommonResponse addNewFunction(SysFunctionParam functionParam) {

        SysProFunInfo sysProFunInfo = new SysProFunInfo();
        sysProFunInfo.setSid(IdUtil.getSnowflakeNextIdStr());
        sysProFunInfo.setFunCode(functionParam.getCode());
        sysProFunInfo.setNickName(functionParam.getNickname());
        sysProFunInfo.setFunType(getNewFunType());

        sysProFunInfo.setCreateTime(LocalDateTime.now());
        sysProFunInfo.setUpdateTime(LocalDateTime.now());
        this.save(sysProFunInfo);

        return CommonResponse.ok("新增功能成功");
    }


    /**
     * 操作父项目的功能管理
     * @param functionParam 总入参
     */
    private  void operaManageFunction(SysFunctionParam functionParam){

        SysProFunInfo sysProFunInfo = new SysProFunInfo();
        sysProFunInfo.setSid(IdUtil.getSnowflakeNextIdStr());
        sysProFunInfo.setCreateTime(LocalDateTime.now());
        sysProFunInfo.setUpdateTime(LocalDateTime.now());
        // TODO: 2023-12-20 在网关过滤器位置获取当前登录用户
        sysProFunInfo.setFunCode(functionParam.getCode());
        sysProFunInfo.setNickName(functionParam.getNickname());
        sysProFunInfo.setFunType(functionParam.getFunType());

        this.save(sysProFunInfo);

        //保存当前功能与项目的关系
        iSysProFunRelationService.saveProFunRelation(sysProFunInfo.getSid(),functionParam.getProSid());

    }

    /**
     * 获取功能type值，初始值为1000，后续自动加1
     * @return
     */
    private Integer getNewFunType(){
        SysProFunInfo proFunInfo = this.getOne(new QueryWrapper<SysProFunInfo>().orderByDesc("create_time").last("limit 1"));
        if (Objects.nonNull(proFunInfo)){
            return proFunInfo.getFunType();
        }else {
            return 1000;
        }
    }





}
